//=============================================================================
// flakshell used as base.
//=============================================================================
/* MQT
  "Was that BEFORE OR AFTER you noticed you were standing in a lake of GASOLINE,
  YOU IDIOT?"
*/
class mine extends ExplosiveMine;

var vector initialDir;

var float ExplodeTimer;
var bool bCanHitOwner, bHitWater;
var() float DampenFactor, DampenFactorParallel;
var class<xEmitter> HitEffectClass;
var float LastSparkTime;
var bool bTimerSet;
var bool bOnWall;
var vector surfaceNormal;
var bool bGoBoom;

replication
{
    reliable if (Role==ROLE_Authority)
        ExplodeTimer;
}

simulated function PostBeginPlay()
{
    Super.PostBeginPlay();

    if ( Role == ROLE_Authority )
    {
        Velocity = Speed * Vector(Rotation);
        RandSpin(25000);
        bCanHitOwner = false;
        if (Instigator.HeadVolume.bWaterVolume)
        {
            bHitWater = true;
            Velocity = 0.6*Velocity;
        }
    }
    if ( Instigator != None ){
		InstigatorController = Instigator.Controller;
		// allows us the option to not blow up team mates
        if(Level.Game.bTeamGame) Team = Instigator.GetTeamNum();
    }
}

simulated function PostNetBeginPlay()
{
	if ( Physics == PHYS_None )
    {
        SetTimer(ExplodeTimer, false);
        bTimerSet = true;
    }
    surfaceNormal = vect(0,0,1);
}

function Timer(){
    noRepExplode(Location, surfaceNormal);
}


event TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, Vector momentum, class<DamageType> damageType)
{
	noRepExplode(Location, surfaceNormal);
}

function detonateMine(){
  noRepExplode(Location, surfaceNormal);
}

simulated function ProcessTouch( actor Other, vector HitLocation )
{
    if ( Pawn(Other) != None && (Other != Instigator || bCanHitOwner) && !bOnWall)
    {
        bTimerSet = true;
		setPhysics(PHYS_None);
        SetBase(Other);//Explode(HitLocation, Normal(HitLocation-Other.Location));
    }
}


simulated function SpawnEffects( vector HitLocation, vector HitNormal )
{
	PlaySound (Sound'WeaponSounds.BExplosion1',,3*TransientSoundVolume);
	if ( EffectIsRelevant(Location,false) )
	{
		spawn(class'FlakExplosion',,,HitLocation + HitNormal*16 );
		spawn(class'FlashExplosion',,,HitLocation + HitNormal*16 );
		spawn(class'GraySmokeRing_Short',,,HitLocation + HitNormal*16, rotator(HitNormal) );
		if ( (ExplosionDecal != None) && (Level.NetMode != NM_DedicatedServer) )
			Spawn(ExplosionDecal,self,,HitLocation, rotator(-HitNormal));
	}
}

simulated function Landed( vector HitNormal ){
    HitWall( HitNormal, None );
}

simulated function HitWall( vector HitNormal, actor Wall ){

    if (!bTimerSet)
    {
        SetTimer(ExplodeTimer, false);
        bTimerSet = true;
    }
    bOnWall=true;
    surfaceNormal = HitNormal;
    SetRotation(rotator(HitNormal));
    bBounce = False;
    SetPhysics(PHYS_None);
    SetBase(Wall);

	if ( (Level.NetMode != NM_DedicatedServer) && (Speed > 250) )
			PlaySound(ImpactSound, SLOT_Misc );
}

simulated function Explode(vector HitLocation, vector HitNormal)
{
    SpawnEffects(HitLocation, HitNormal );
    //bGoBoom=true;
	if ( Role == ROLE_Authority )
	{
		td_noSafetyHurtRadius(damage, DamageRadius, MyDamageType, MomentumTransfer, HitLocation);
	}
    Destroy();
}

function noRepExplode(vector HitLocation, vector HitNormal)
{
    spawn(class'mineBoom',,,HitLocation + HitNormal*16 );
    PlaySound (Sound'WeaponSounds.BExplosion1',,3*TransientSoundVolume);
    //bGoBoom=true;
    td_noSafetyHurtRadius(damage, DamageRadius, MyDamageType, MomentumTransfer, HitLocation);
    Destroy();
}

/* HurtRadius()
 Hurt locally authoritative actors within the radius.
*/
final function td_noSafetyHurtRadius( float DamageAmount, float DamageRadius, class<DamageType> DamageType, float Momentum, vector HitLocation )
{
	local actor Victims;
	local float damageScale, dist;
	local vector dir;

	if( bHurtEntry )
		return;

	bHurtEntry = true;
	foreach CollidingActors( class 'Actor', Victims, DamageRadius, HitLocation )
	{
		// don't let blast damage affect fluid - VisibleCollisingActors doesn't really work for them - jag
		if( (Victims != self) && (Victims.Role == ROLE_Authority) && (!Victims.IsA('FluidSurfaceInfo')) )
		{
			dir = Victims.Location - HitLocation;
			dist = FMax(1,VSize(dir));
			dir = dir/dist;
			damageScale = 1 - FMax(0,(dist - Victims.CollisionRadius)/DamageRadius);
			Victims.TakeDamage
			(
				damageScale * DamageAmount,
				Instigator,
				Victims.Location - 0.5 * (Victims.CollisionHeight + Victims.CollisionRadius) * dir,
				(damageScale * Momentum * dir),
				DamageType
			);
		}
	}
	bHurtEntry = false;
}

defaultproperties
{
     ExplodeTimer=10.000000
     DampenFactor=0.500000
     DampenFactorParallel=0.200000
     Speed=950.000000
     TossZ=200.000000
     Damage=50.000000
     MomentumTransfer=75000.000000
     MyDamageType=Class'taskforces.DamTypeProxyMine'
     ImpactSound=Sound'WeaponSounds.BaseGunTech.BLockOn1'
     ExplosionDecal=Class'XEffects.RocketMark'
     DrawType=DT_StaticMesh
     StaticMesh=StaticMesh'TFMedia.explosive.GEMine_stat'
     bNetTemporary=False
     bOnlyDirtyReplication=True
     Physics=PHYS_Falling
     LifeSpan=0.000000
     DrawScale=0.450000
     AmbientGlow=40
     bHardAttach=True
     SoundVolume=255
     SoundRadius=100.000000
     CollisionRadius=2.000000
     CollisionHeight=2.000000
     bProjTarget=True
     bUseCollisionStaticMesh=True
     ForceType=FT_Constant
     ForceRadius=60.000000
     ForceScale=5.000000
}
